package jfc.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
import jfc.core.*;
import javax.swing.event.MouseInputAdapter;
import jfc.io.FileManager;

/**
 *  @author Yann Le Gall
 *  ylegall@gmail.com
 *  Oct 25, 2009 8:41:52 PM
 */
public class GamePanel extends JPanel {

    public static final int CARDGAP = 30;
    private final GameManager gameManager;
    private BufferedImage image;

    public GamePanel() {
        gameManager = new GameManager();
        setBackground(new Color(0, 150, 0));
        CardListener cl = new CardListener();
        addMouseListener(cl);
        addMouseMotionListener(cl);
    }
    
    public void newGame(MainWindow window) {
        gameManager.newGame(window);
            new Thread(new Runnable(){ public void  run() {
                try {
                    FileManager.save(gameManager, new File("start.fcl"));
                } catch (IOException e) {}
            }}).start();
    }
    
    public void undo() {gameManager.undo(); repaint();}

    @Override
    public Dimension getPreferredSize() {
        Dimension d = new Dimension(Card.width() * 8 + 50, (Card.height() << 1) + 20);
        int maxCards = gameManager.maxColSize();
        d.height = d.height + (CARDGAP * maxCards);
        return d;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gameManager.draw(g2, true);
        g2.dispose();
    }
    
    public void paintBackgroundCards(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gameManager.draw(g2, false);
        g2.dispose();
    }

    public void setImage() {
    	if(image == null)
    		image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
    	paintBackgroundCards(g);
        g.dispose();
    }

    public void paintPanel(Graphics2D g2) {
//        Graphics2D g2 = (Graphics2D)getGraphics();
//        g2.drawImage(image, 0, 0, this);
//        g2.setClip(gameManager.getClip());
//        gameManager.drawCards(g2, false);
//        g2.dispose();

        g2.drawImage(image, 0, 0, this);
        g2.setClip(gameManager.getClip());
        gameManager.drawCards(g2, false);
    }

    private class CardListener extends MouseInputAdapter
    {
        @Override
        public void mouseClicked(MouseEvent me) {
            Point p = me.getPoint();
            gameManager.performAction(p);
            repaint();
        }
    }

    public boolean save() {
        try {
            return FileManager.save(gameManager);
        }
        catch (IOException e) { return false;}
    }
}
